package com.block.api.controller.merchant;

import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantDetailInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/merchant")
@Validated
public class MerchantController {

    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;
    @Autowired
    IMerchantDetailInfoService merchantDetailInfoService;

    /**
     * 根据定位信息
     * 查询商户列表
     */
    @RequestMapping(value = "/getInfoList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getPageList(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String cityCode = tranData.getString("cityCode");

        ResponseVo responseVo = merchantBaseInfoService.getPageDataList(cityCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 查询商户详情
     */
    @NeedToken
    @RequestMapping(value = "/getDetailInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getDetailInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        String infoId = tranData.getString("infoId");

        ResponseVo responseVo = merchantDetailInfoService.getDetailInfoByInfoId(infoId);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

}
